package com.tsxy.mall.controller;

import com.tsxy.mall.entity.Order;
import com.tsxy.mall.entity.User;
import com.tsxy.mall.service.AddressService;
import com.tsxy.mall.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private AddressService addressService;
    @RequestMapping("/add")
    public String add(@RequestParam("cartId") Integer[] cartIds, HttpSession session, Model model){
        User user = (User) session.getAttribute("user");
        if (cartIds.length == 0){

        }
        session.setAttribute("cartIds",cartIds);
        Order order = orderService.add(cartIds, user.getId());
        model.addAttribute("order",order);
        // 查询用户的地址信息
        List addresses = addressService.getByUserId(user.getId());
        model.addAttribute("addresss",addresses);
        return "user/order";
    }

    @RequestMapping("/list")
    public String toList(){
        return "user/orderList";
    }
}
